package com.eastedu.superise.handler.preparing;

import cn.hutool.core.collection.CollStreamUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.eastedu.repository.lesson.mapper.PreparationMapper;
import com.eastedu.repository.lesson.po.PreparationPO;
import com.eastedu.superise.handler.AbstractHistoricalDataHandler;
import com.eastedu.superise.handler.common.ResourceUpdateTimeService;
import com.eastedu.utils.JsonUtil;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.List;

/**
 * @author 袁松明
 */
@Slf4j
@Service
@RequiredArgsConstructor
@DS("lesson")
public class PreparingUpdateTimeHistoricalDataHandler extends AbstractHistoricalDataHandler<PreparationPO> {
    private final PreparationMapper preparationMapper;
    private final ResourceUpdateTimeService<PreparationPO> resourceUpdateTimeService;


    @Override
    protected String getSql() {
        return "SELECT * FROM t_preparation";
    }

    @Override
    public void callback(PreparationPO po) throws SQLException {
        List<String> sourceChapterIds = CollStreamUtil.toList(JsonUtil.parseList(po.getChapters(), Chapter.class), Chapter::getChapterId);
        resourceUpdateTimeService.update(preparationMapper, po, sourceChapterIds);
    }


    @NoArgsConstructor
    @Data
    private static class Chapter {
        private Long id;
        private String chapterId;
        private Long createTime;
        private Long updateTime;
        private String chapterName;
        private String chapterPath;
        private String chapterRootId;
        private Long preparationId;
        private String chapterRootName;
    }
}
